<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateConfigAPIRequest;
use App\Http\Requests\API\UpdateConfigAPIRequest;
use App\Models\Category;
use App\Models\Config;
use App\Models\Label;
use App\Repositories\CategoryRepository;
use App\Repositories\ConfigRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class ConfigController
 * @package App\Http\Controllers\API
 */

class ConfigAPIController extends AppBaseController
{
    /** @var  ConfigRepository */
    private $configRepository;

    public function __construct(ConfigRepository $configRepo)
    {
        $this->configRepository = $configRepo;
    }

    /**
     * Display a listing of the Config.
     * GET|HEAD /configs
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->configRepository->pushCriteria(new RequestCriteria($request));
        $this->configRepository->pushCriteria(new LimitOffsetCriteria($request));
        $configs = $this->configRepository->all();

        return $this->sendResponse($configs->toArray(), 'Configs retrieved successfully');
    }

    /**
     * Store a newly created Config in storage.
     * POST /configs
     *
     * @param CreateConfigAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateConfigAPIRequest $request)
    {
        $input = $request->all();

        $configs = $this->configRepository->create($input);

        return $this->sendResponse($configs->toArray(), 'Config saved successfully');
    }

    /**
     * Display the specified Config.
     * GET|HEAD /configs/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var Config $config */
        $config = $this->configRepository->findWithoutFail($id);

        if (empty($config)) {
            return $this->sendError('Config not found');
        }

        return $this->sendResponse($config->toArray(), 'Config retrieved successfully');
    }

    /**
     * Update the specified Config in storage.
     * PUT/PATCH /configs/{id}
     *
     * @param  int $id
     * @param UpdateConfigAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateConfigAPIRequest $request)
    {
        $input = $request->all();

        /** @var Config $config */
        $config = $this->configRepository->findWithoutFail($id);

        if (empty($config)) {
            return $this->sendError('Config not found');
        }

        $config = $this->configRepository->update($input, $id);

        return $this->sendResponse($config->toArray(), 'Config updated successfully');
    }

    /**
     * Remove the specified Config from storage.
     * DELETE /configs/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var Config $config */
        $config = $this->configRepository->findWithoutFail($id);

        if (empty($config)) {
            return $this->sendError('Config not found');
        }

        $config->delete();

        return $this->sendResponse($id, 'Config deleted successfully');
    }

    public function getConfigsByGroup($group='system')
    {
        $data = $this->configRepository->getMapByGroup($group);
        return responseZK(1,$data,'成功');
    }

    public function cateIndex(Request $request)
    {
        $user = Auth::user();
        $roles = $user->roles;
        $cate_ids = [];
        foreach ($roles as $role) {
            $cate_ids = array_merge($cate_ids, $role->categories()->pluck('category_id')->toArray());
        }
        $pid = $request->pid;
        $cate_ids = array_merge($cate_ids, Category::whereIn('id',$cate_ids)->pluck('pid')->toArray());
        if($pid){
            $cates = Category::where('pid',$pid)->whereIn('id',$cate_ids)->orderBy('sort','asc')->where('is_show',1)->get();
        }else{
            $cates = Category::where('level',1)->whereIn('id',$cate_ids)->orderBy('sort','asc')->where('is_show',1)->get();
//            $ca = app()->build(CategoryRepository::class);
//            $cates = $ca->getLevelData(1, 1, $cate_ids);
        }
        return responseZK(1,$cates,'success');
    }

    public function labelIndex(Request $request)
    {
        $user = Auth::user();
        $roles = $user->roles;
        $label_ids = [];
        foreach ($roles as $role) {
            $label_ids = array_merge($label_ids, $role->labels()->pluck('label_id')->toArray());
        }
        $type = $request->type;
        $model = new Label();
        $model = $model->whereIn('id',$label_ids)->orderBy('sort','asc');
        if($type){
            $model = $model->where('type',$type);
        }
        $labels = $model->get();

        return responseZK(1,$labels,'success');
    }
}
